import React,{Component} from 'react'
import classNames from 'classnames/bind'
import styles from './login.scss'
import { Form, Icon, Input, Button, Checkbox } from 'antd';
import axios from 'axios';
import {API_ADDRESS} from '../util/address';


let cx=classNames.bind(styles)
const FormItem = Form.Item;

class LoginForm extends Component {
  componentWillMount()
  {
    if(sessionStorage.getItem('username'))
    {
      location.replace('/admin/werenshi/postedit')
    }
  }
   handleSubmit(e) {
    e.preventDefault();
    this.props.form.validateFields(async (err, values) => {
      if (!err) {
        let username=values.userName
        let password=values.password
        let options = {
            username,
            password
        }
        const {data:{login}}=await axios.post(`${API_ADDRESS}/login`,options)
        if(login==='error'){
          alert('登录失败！')
        }
        else{
          sessionStorage.setItem('username','微人事管理员')
          location.replace('/admin/werenshi/postedit')
        }
      }
    });
  }
  render() {
    const { getFieldDecorator } = this.props.form;
    return (
        <div>
        <div class={cx('login')}>
        <Form onSubmit={this.handleSubmit.bind(this)} className="login-form">
          <FormItem>
            {getFieldDecorator('userName', {
              rules: [{ required: true, message: '请输入用户名!' }],
            })(
              <Input prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} placeholder="用户名" />
            )}
          </FormItem>
          <FormItem>
            {getFieldDecorator('password', {
              rules: [{ required: true, message: '请输入密码!' }],
            })(
              <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} type="password" placeholder="密码" />
            )}
          </FormItem>
          {/* <FormItem>
            {getFieldDecorator('remember', {
              valuePropName: 'checked',
              initialValue: true,
            })(
              <Checkbox>记住用户名和密码</Checkbox>
            )}
          </FormItem> */}
          <FormItem>
            <Button type="primary" htmlType="submit" className="login-form-button">
              登录
            </Button>
          </FormItem>
        </Form>
      </div>
      </div>
    );
  }
}
LoginForm = Form.create()(LoginForm);
export default LoginForm;
